package com.beidouin.GnssMain.sample;

import android.support.annotation.IdRes;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.CoordinateConverter;
import com.beidouin.GnssMain.EventSerialReceive;
import com.beidouin.GnssMain.FileManager;
import com.beidouin.GnssMain.R;
import com.beidouin.GnssMain.models.PilotInfo;


public class MapActivity extends SerialPortActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {


    private MapView mMapView = null;
    private BaiduMap mBaiduMap;
    private Button reset;
    private RadioGroup mapStyle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_map);
        initBottomView();
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(15));
        mBaiduMap.setMaxAndMinZoomLevel(20f, 4f);
        mapImage.setImageResource(R.drawable.icon_map_select);
        mapText.setTextColor(getResources().getColor(R.color.app_main));

        mapStyle = (RadioGroup) findViewById(R.id.map_style);
        mapStyle.setOnCheckedChangeListener(this);
        reset = (Button) findViewById(R.id.reset);
        reset.setOnClickListener(this);

//        defaultvalidateUI();
        if (PilotInfo.getInstance().getLongitude() != null && PilotInfo.getInstance().getLatitude() != null) {
            validateUI();
        }
    }

    private void validateUI() {

        double x = 116.46;
        double y = 39.92;

        if (!TextUtils.isEmpty(PilotInfo.getInstance().getLongitude() ) && !TextUtils.isEmpty(PilotInfo.getInstance().getLatitude()))  {

            x = PilotInfo.getInstance().getLongitude_double();
            y = PilotInfo.getInstance().getLatitude_double();
            Log.i("x", "===================" + x);
            LatLng point = new LatLng(y, x);

            CoordinateConverter converter = new CoordinateConverter();
            converter.from(CoordinateConverter.CoordType.GPS);
            // sourceLatLng待转换坐标
            converter.coord(point);
            LatLng desLatLng = converter.convert();
            point = desLatLng;
            BitmapDescriptor bitmap = BitmapDescriptorFactory
                    .fromResource(R.drawable.map_sign);
            OverlayOptions option = new MarkerOptions()
                    .position(point)
                    .icon(bitmap);
            mBaiduMap.addOverlay(option);
            mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(point));
        }
        if (FileManager.getInstance().get_FileStreamSta() == FileManager.ISPLAYING) {
            double x2;
            double y2;
            x2 = PilotInfo.getInstance().getLongitude_FromPC_double();
            y2 = PilotInfo.getInstance().getLatitude_FromPC_double();
            LatLng point2 = new LatLng(y2, x2);
            CoordinateConverter converter2 = new CoordinateConverter();
            converter2.from(CoordinateConverter.CoordType.GPS);
            // sourceLatLng待转换坐标
            converter2.coord(point2);
            LatLng desLatLng2 = converter2.convert();
            point2 = desLatLng2;
            BitmapDescriptor bitmap2 = BitmapDescriptorFactory
                    .fromResource(R.drawable.map_sign);
            OverlayOptions option2 = new MarkerOptions()
                    .position(point2)
                    .icon(bitmap2);
            mBaiduMap.addOverlay(option2);
            mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(point2));
        }
    }

    private void defaultvalidateUI() {////////在oncret中调用，产生默认点

        double x;
        double y;
        x = 116.46;
        y = 39.92;
        LatLng point = new LatLng(y, x);
        CoordinateConverter converter = new CoordinateConverter();
        converter.from(CoordinateConverter.CoordType.GPS);
        // sourceLatLng待转换坐标
        converter.coord(point);
        LatLng desLatLng = converter.convert();
        point = desLatLng;
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.map_sign);
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
        mBaiduMap.addOverlay(option);
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(point));
//        if(FileManager.getInstance().get_FileStreamSta()==FileManager.ISPLAYING) {
//            double x2;
//            double y2;
//            x2 = PilotInfo.getInstance().getLongitude_FromPC_double();
//            y2 = PilotInfo.getInstance().getLatitude_FromPC_double();
//            LatLng point2 = new LatLng(y2, x2);
//            CoordinateConverter converter2 = new CoordinateConverter();
//            converter2.from(CoordinateConverter.CoordType.GPS);
//            // sourceLatLng待转换坐标
//            converter2.coord(point2);
//            LatLng desLatLng2 = converter2.convert();
//            point2 = desLatLng2;
//            BitmapDescriptor bitmap2 = BitmapDescriptorFactory
//                    .fromResource(R.drawable.map_sign2);
//            OverlayOptions option2 = new MarkerOptions()
//                    .position(point2)
//                    .icon(bitmap2);
//            //mBaiduMap.addOverlay(option2);
//            mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(point2));
//        }
    }

    public void onEventMainThread(final EventSerialReceive event) {
        if (event.eventType == EventSerialReceive.EVENT_UPDATE_ACTIVITY) { //data from serial port
            if ((event.iData & EventSerialReceive.UPDATE_MAP) != 0) {
                //validateUI();
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (group.getCheckedRadioButtonId()) {
            case R.id.map_1:
                mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
                break;
            case R.id.map_2:
                mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reset:

                if (mBaiduMap != null)
                    mBaiduMap.clear();


                break;
        }
    }

}



